package com.yykj.order.center.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yykj.common.util.LocalStringUtil;
import com.yykj.hospital.client.dto.consultation.template.ConsultationVerifyDTO;
import com.yykj.hospital.client.feign.HospitalServerFeignClient;
import com.yykj.hospital.client.form.consultation.ConsultationVerifyForm;
import com.yykj.order.center.client.dto.*;
import com.yykj.order.center.client.dto.base.PaginationListDto;
import com.yykj.order.center.client.form.consultation.*;
import com.yykj.order.center.server.entity.Consultation;
import com.yykj.order.center.server.entity.ConsultationDetail;
import com.yykj.order.center.server.entity.ConsultationDoctor;
import com.yykj.order.center.server.enums.ConsultationDoctorStatus;
import com.yykj.order.center.server.enums.ConsultationStatus;
import com.yykj.order.center.server.exception.ErrorCode;
import com.yykj.order.center.server.exception.ErrorCodeException;
import com.yykj.order.center.server.repository.ConsultationDetailMapper;
import com.yykj.order.center.server.repository.ConsultationDoctorMapper;
import com.yykj.order.center.server.repository.ConsultationMapper;
import com.yykj.order.center.server.service.ConsultationService;
import com.yykj.order.center.server.service.MessageService;
import com.yykj.order.center.server.util.StringUtil;
import com.yykj.tencent.im.client.feign.TencentImFeignClient;
import com.yykj.tencent.im.client.form.CreateGroupForm;
import com.yykj.tencent.im.client.form.CreateGroupResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Transactional
@Component
public class ConsultationServiceImpl implements ConsultationService {

    @Autowired
    ConsultationMapper consultationMapper;

    @Autowired
    ConsultationDetailMapper consultationDetailMapper;

    @Autowired
    ConsultationDoctorMapper consultationDoctorMapper;

    @Autowired
    HospitalServerFeignClient hospitalServerFeignClient;

    @Autowired
    MessageService messageService;

    @Autowired
    TencentImFeignClient tencentImFeignClient;

    @Override
    public ConsultationCreateDto create(ConsultationCreateForm consultationCreateForm) {
        consultationCreateForm.setDoctorIdList(StringUtil.duplicateRemovalList(consultationCreateForm.getDoctorIdList()));
        //新建会诊
        int currentTime = StringUtil.getCurrentTime();
        Consultation consultation = new Consultation();
        BeanUtils.copyProperties(consultationCreateForm, consultation);
        String orderNo = StringUtil.getUUID();
        consultation.setOrderNo(orderNo);
        consultation.setAddtime(currentTime);
        consultation.setId(LocalStringUtil.getUUID());
        this.consultationMapper.insert(consultation);
        String consultationId = consultation.getId();

        //会诊与会医生
        String applyDoctorId = consultationCreateForm.getDoctorId();
        //获取医生详细信息, 包含申请医生一起获取
        List<String> doctorIdList = consultationCreateForm.getDoctorIdList();
        doctorIdList.add(applyDoctorId);
        ConsultationVerifyForm consultationVerifyForm = new ConsultationVerifyForm();
        consultationVerifyForm.setConsultationId(consultationId);
        consultationVerifyForm.setDoctorIds(doctorIdList);
        List<ConsultationDoctor> needSendImDoctorList = new ArrayList<>();//需要发送IM邀请消息的医生列表,过滤掉需要审核的医生
        List<ConsultationVerifyDTO> consultationVerifyDTOS = this.hospitalServerFeignClient.consultationCheck(consultationVerifyForm);
        Iterator<String> iterator = doctorIdList.iterator();
        int sort = 0;
        while (iterator.hasNext()) {
            String doctorId = iterator.next();
            Iterator<ConsultationVerifyDTO> iteratorc = consultationVerifyDTOS.iterator();
            while (iteratorc.hasNext()) {
                ConsultationVerifyDTO consultationVerifyDTO = iteratorc.next();
                String doctorIdc = consultationVerifyDTO.getDoctorId();
                if (doctorId.equals(doctorIdc)) {
                    Integer depId = consultationVerifyDTO.getDepId();
                    String depName = consultationVerifyDTO.getDepName();
                    String doctorName = consultationVerifyDTO.getDoctorName();
                    String headImg = consultationVerifyDTO.getHeadImg();
                    Integer hospitalId = consultationVerifyDTO.getHospitalId();
                    String hospitalName = consultationVerifyDTO.getHospitalName();
                    String mobile = consultationVerifyDTO.getMobile();
                    String officer = consultationVerifyDTO.getOfficer();
                    String sign = consultationVerifyDTO.getSign();
                    String tencentAccountId = consultationVerifyDTO.getTencentAccountId();
                    if (doctorId.equals(applyDoctorId)) {//申请医生
                        int sendCheckStatus = consultationVerifyDTO.getSendCheckStatus();
                        int status = ConsultationStatus.create;
                        if (sendCheckStatus == 1) {//1 需要审核
                            status = ConsultationStatus.review;
                        } else if (sendCheckStatus == 2) {//2 不需要审核
                            status = ConsultationStatus.success;
                        } else {//0 医生自己决定
                            if (1 == consultationCreateForm.getNeedReview()) {
                                status = ConsultationStatus.review;
                            } else {
                                status = ConsultationStatus.success;
                            }
                        }
                        consultation.setStatus(status);
                        consultation.setApplyDoctorId(doctorId);
                        consultation.setApplyDoctorName(doctorName);
                        consultation.setApplyDoctorImId(tencentAccountId);
                        consultation.setApplyDoctorHospitalId(hospitalId);
                        consultation.setApplyDoctorHospitalName(hospitalName);
                        consultation.setApplyDoctorDepartmentId(depId);
                        consultation.setApplyDoctorDepartmentName(depName);
                        consultation.setMobile(mobile);
                        consultation.setOfficer(officer);
                        consultation.setSign(sign);
                        consultation.setHeadImg(headImg);
                    } else {//添加 与会医生
                        ConsultationDoctor consultationDoctor = this.consultationVerifyDTO2ConsultationDoctor(consultationVerifyDTO);
                        consultationDoctor.setConsultationId(consultationId);
                        consultationDoctor.setAddtime(currentTime);
                        consultationDoctor.setSort(sort++);
                        this.consultationDoctorMapper.insert(consultationDoctor);
                        if (consultationDoctor.getStatus() == 1) {//直接进入会诊的 需要发消息
                            needSendImDoctorList.add(consultationDoctor);
                        }
                    }
                    iterator.remove();
                    iteratorc.remove();
                    break;
                }
            }
        }
        if (doctorIdList.size() > 0) {//需要获取的医生数量 和 微服务查出来的医生数量不一致
            throw new ErrorCodeException(ErrorCode._1005);
        }

        //会诊自定义字段
        List<ConsultationCreateDetailForm> consultationDetailList = consultationCreateForm.getConsultationDetailList();
        if (consultationDetailList != null && consultationDetailList.size() > 0) {
            for (int i = 0; i < consultationDetailList.size(); i++) {
                ConsultationDetail consultationDetail = new ConsultationDetail();
                BeanUtils.copyProperties(consultationDetailList.get(i), consultationDetail);
                consultationDetail.setConsultationId(consultationId);
                consultationDetail.setSort(i);
                consultationDetail.setId(LocalStringUtil.getUUID());
                this.consultationDetailMapper.insert(consultationDetail);
            }
        }
        String groupId = null;
        if (consultation.getStatus() == ConsultationStatus.success) {//无需审核直接创建
            //建群聊天室
            consultation.setGroupImId(createGroup(consultation));
            ArrayList<String> memberList = new ArrayList<>();
            //发送邀请医生信息
            for (ConsultationDoctor consultationDoctor : needSendImDoctorList) {
                memberList.add(consultationDoctor.getDoctorImId());
                this.messageService.sendMeg(consultation.getApplyDoctorImId(), consultationDoctor.getDoctorImId(),
                        getAddConsultationDoctorContent(consultation.getApplyDoctorName(), consultationDoctor.getDoctorName()));
            }

            this.messageService.memberAdd(consultation.getGroupImId(), memberList);//添加成员
        }
        this.consultationMapper.updateById(consultation);
        return ConsultationCreateDto.builder()
                .groupImId(groupId)
                .consultationId(consultationId)
                .build();
    }

    @Override
    public String createGroup(Consultation consultation) {
//        return "cc-" + StringUtil.getRandom();
        String applyDoctorImId = consultation.getApplyDoctorImId();
        String groupName = consultation.getTypeName() + "：" + consultation.getPatientName();
        CreateGroupForm createGroupForm = CreateGroupForm.builder()
                .faceUrl(consultation.getFaceUrl())
                .name(groupName)
                .ownerAccount(applyDoctorImId)
                .type("Private")
                .build();
        CreateGroupResult createGroupResult = this.tencentImFeignClient.create(createGroupForm);
        if (createGroupResult == null || StringUtil.isBlank(createGroupResult.getGroupId())) {
            throw new ErrorCodeException(ErrorCode._1004);
        }
        return createGroupResult.getGroupId();
    }

    @Override
    public int countByDoctor(String doctorId, int startTime, int endTime) {
        return this.consultationMapper.countByDoctor(doctorId, startTime, endTime);
    }

    public static String getAddConsultationDoctorContent(String applyDoctorName, String doctorName) {
        //某某（邀请医生姓名）邀请某某（被邀请医生姓名）参与会诊
        return applyDoctorName + " 邀请 " + doctorName + " 参与会诊";
    }

    public String getRemoveConsultationDoctorContent(String applyDoctorName, String doctorName) {
        return applyDoctorName + " 移除 " + doctorName + " 会诊";
    }

    public static String getFinishConsultationDoctorContent(String applyDoctorName, String doctorName) {
        return "会诊已结束";
    }

    //微服务返回的 consultationVerifyDTO 转换成 ConsultationDoctor
    public ConsultationDoctor consultationVerifyDTO2ConsultationDoctor(ConsultationVerifyDTO consultationVerifyDTO) {
        int status = consultationVerifyDTO.getReceiveCheckStatus() == 1 ? ConsultationDoctorStatus.review : ConsultationDoctorStatus.success;
        return ConsultationDoctor.builder()
                .id(LocalStringUtil.getUUID())
                .status(status)
                .doctorDepartmentId(consultationVerifyDTO.getDepId())
                .doctorDepartmentName(consultationVerifyDTO.getDepName())
                .doctorHospitalId(consultationVerifyDTO.getHospitalId())
                .doctorHospitalName(consultationVerifyDTO.getHospitalName())
                .doctorId(consultationVerifyDTO.getDoctorId())
                .doctorName(consultationVerifyDTO.getDoctorName())
                .doctorImId(consultationVerifyDTO.getTencentAccountId())
                .headImg(consultationVerifyDTO.getHeadImg())
                .mobile(consultationVerifyDTO.getMobile())
                .officer(consultationVerifyDTO.getOfficer())
                .sign(consultationVerifyDTO.getSign()).build();
    }

    @Override
    public PaginationListDto<ConsultationListDto> list(ConsultationListForm consultationListForm) {
        //自己发起的全状态 + 被邀请的 会诊中 和 完成的
        PageHelper.startPage(consultationListForm.getPageNum(), consultationListForm.getPageSize());
        Page<Consultation> consultationList = this.consultationMapper.selectByDoctor(consultationListForm.getDoctorId(), consultationListForm.getStatus());
        ArrayList<ConsultationListDto> consultationListDtos = new ArrayList<>();
        for (Consultation consultation : consultationList) {
            if(consultation != null){
                ConsultationListDto consultationListDto = new ConsultationListDto();
                BeanUtils.copyProperties(consultation, consultationListDto);
                consultationListDto.setConsultationId(consultation.getId());
                consultationListDtos.add(consultationListDto);
            }
        }
        PaginationListDto<ConsultationListDto> consultationListDtoPaginationListDto = new PaginationListDto<>();
        consultationListDtoPaginationListDto.setList(consultationListDtos);
        consultationListDtoPaginationListDto.setPageNum(consultationListForm.getPageNum());
        consultationListDtoPaginationListDto.setPageSize(consultationListForm.getPageSize());
        consultationListDtoPaginationListDto.setTotal(consultationList.getTotal());
        return consultationListDtoPaginationListDto;
    }

    @Override
    public void chargeback(ConsultationChargebackForm consultationChargebackForm) {
        Consultation consultation = this.consultationMapper.findById(consultationChargebackForm.getConsultationId());
        consultation.setStatus(ConsultationStatus.reviewChargeback);
        consultation.setChargebackRemark(consultationChargebackForm.getRemark());
        this.consultationMapper.updateById(consultation);
    }

    @Override
    public void addDoctor(ConsultationAddDoctorForm consultationAddDoctorForm) {
        consultationAddDoctorForm.setDoctorIdList(StringUtil.duplicateRemovalList(consultationAddDoctorForm.getDoctorIdList()));
        Consultation consultation = this.consultationMapper.findById(consultationAddDoctorForm.getConsultationId());
        List<String> doctorIdList = consultationAddDoctorForm.getDoctorIdList();
        doctorIdList.remove(consultation.getApplyDoctorId());//去除自己
        ConsultationVerifyForm consultationVerifyForm = new ConsultationVerifyForm();
        consultationVerifyForm.setConsultationId(consultationAddDoctorForm.getConsultationId());
        consultationVerifyForm.setDoctorIds(doctorIdList);
        List<ConsultationDoctor> needSendImDoctorList = new ArrayList<>();//需要发送IM邀请消息的医生列表,过滤掉需要审核的医生
        List<ConsultationVerifyDTO> consultationVerifyDTOS = this.hospitalServerFeignClient.consultationCheck(consultationVerifyForm);
        Iterator<String> iterator = doctorIdList.iterator();
        int sort = this.consultationDoctorMapper.findMaxSortByConsultationId(consultationAddDoctorForm.getConsultationId());
        int currentTime = StringUtil.getCurrentTime();
        while (iterator.hasNext()) {
            String doctorId = iterator.next();
            Iterator<ConsultationVerifyDTO> iteratorc = consultationVerifyDTOS.iterator();
            while (iteratorc.hasNext()) {
                ConsultationVerifyDTO consultationVerifyDTO = iteratorc.next();
                String doctorIdc = consultationVerifyDTO.getDoctorId();
                if (doctorId.equals(doctorIdc)) {
                    //如果已经存在 更改状态

                    ConsultationDoctor consultationDoctorCurrent = this.consultationDoctorMapper.findByConsultationIdAndDoctorId(consultationAddDoctorForm.getConsultationId(), doctorIdc);
                    if (consultationDoctorCurrent != null) {
                        //已经审核通过或者直接可以进的直接变成进入
                        int status = consultationDoctorCurrent.getStatus();
                        if(status == ConsultationDoctorStatus.reviewFail){
                            consultationDoctorCurrent.setStatus(ConsultationDoctorStatus.review);
                        }else if(status == ConsultationDoctorStatus.review){

                        }else if(status == ConsultationDoctorStatus.reviewSuccess){

                        }else{
                            consultationDoctorCurrent.setStatus(ConsultationDoctorStatus.success);
                        }

                        this.consultationDoctorMapper.updateById(consultationDoctorCurrent);
                        if (consultationDoctorCurrent.getStatus() == ConsultationDoctorStatus.success) {
                            needSendImDoctorList.add(consultationDoctorCurrent);
                        }
                    }else{
                        ConsultationDoctor consultationDoctor = this.consultationVerifyDTO2ConsultationDoctor(consultationVerifyDTO);

                        consultationDoctor.setConsultationId(consultationAddDoctorForm.getConsultationId());
                        consultationDoctor.setAddtime(currentTime);
                        consultationDoctor.setSort(++sort);
                        try {//唯一约束
                            this.consultationDoctorMapper.insert(consultationDoctor);
                            if (consultationDoctor.getStatus() == ConsultationDoctorStatus.success) {//直接进入会诊的 需要发消息
                                needSendImDoctorList.add(consultationDoctor);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    iterator.remove();
                    iteratorc.remove();
                    break;
                }

            }
        }
        ArrayList<String> memberList = new ArrayList<>();
        for (ConsultationDoctor consultationDoctor : needSendImDoctorList) {
            memberList.add(consultationDoctor.getDoctorImId());
            this.messageService.sendMeg(consultation.getApplyDoctorImId(), consultationDoctor.getDoctorImId(),
                    getAddConsultationDoctorContent(consultation.getApplyDoctorName(), consultationDoctor.getDoctorName()));
        }
        this.messageService.memberAdd(consultation.getGroupImId(), memberList);//添加成员
    }

    @Override
    public List<ConsultationDoctorDto> doctorList(String consultationId) {
        Consultation consultation = this.consultationMapper.findById(consultationId);
        List<ConsultationDoctor> consultationDoctorList = this.consultationDoctorMapper.findByConsultationId(consultationId);
        ArrayList<ConsultationDoctorDto> consultationDoctorDtos = new ArrayList<>();
        //加上申请医生一起 type=1
        consultationDoctorDtos.add(ConsultationDoctorDto.builder()
                .type(1)
                .consultationId(consultation.getId())
                .doctorDepartmentId(consultation.getApplyDoctorDepartmentId())
                .doctorDepartmentName(consultation.getApplyDoctorDepartmentName())
                .doctorHospitalId(consultation.getApplyDoctorHospitalId())
                .doctorHospitalName(consultation.getApplyDoctorHospitalName())
                .doctorId(consultation.getApplyDoctorId())
                .doctorImId(consultation.getApplyDoctorImId())
                .doctorName(consultation.getApplyDoctorName())
                .sign(consultation.getSign())
                .headImg(consultation.getHeadImg()).build());
        for (ConsultationDoctor consultationDoctor : consultationDoctorList) {
            ConsultationDoctorDto consultationDoctorDto = new ConsultationDoctorDto();
            BeanUtils.copyProperties(consultationDoctor, consultationDoctorDto);
            consultationDoctorDtos.add(consultationDoctorDto);
        }
        return consultationDoctorDtos;
    }

    @Override
    public void removeDoctor(ConsultationRemoveDoctorForm consultationRemoveDoctorForm) {
        Consultation consultation = this.consultationMapper.findById(consultationRemoveDoctorForm.getConsultationId());

        ConsultationDoctor consultationDoctor = this.consultationDoctorMapper.findByConsultationIdAndDoctorId(consultationRemoveDoctorForm.getConsultationId(), consultationRemoveDoctorForm.getRemoveDoctorId());
        if (consultationDoctor == null) {
            throw new ErrorCodeException(ErrorCode._1002);
        }
        consultationDoctor.setStatus(ConsultationDoctorStatus.remove);
        consultationDoctor.setRemoveRemark(consultationRemoveDoctorForm.getRemark());
        consultationDoctor.setRemoveTime(StringUtil.getCurrentTime());
        this.consultationDoctorMapper.updateById(consultationDoctor);

        ArrayList<String> memberList = new ArrayList<>();
        memberList.add(consultationDoctor.getDoctorImId());
        this.messageService.memberDelete(consultation.getGroupImId(), consultationRemoveDoctorForm.getRemark(), memberList);
        this.messageService.sendMeg(consultation.getApplyDoctorImId(), consultationDoctor.getDoctorImId(),
                getRemoveConsultationDoctorContent(consultation.getApplyDoctorName(), consultationDoctor.getDoctorName()));
    }

    @Override
    public void doctorQuit(ConsultationDoctorQuitForm consultationDoctorQuitForm) {
        Consultation consultation = this.consultationMapper.findById(consultationDoctorQuitForm.getConsultationId());
        ConsultationDoctor consultationDoctor = this.consultationDoctorMapper.findByConsultationIdAndDoctorId(consultationDoctorQuitForm.getConsultationId(), consultationDoctorQuitForm.getDoctorId());
        if (consultationDoctor == null) {
            throw new ErrorCodeException(ErrorCode._1002);
        }
        consultationDoctor.setStatus(ConsultationDoctorStatus.quit);
        consultationDoctor.setQuitRemark(consultationDoctorQuitForm.getRemark());
        consultationDoctor.setQuitTime(StringUtil.getCurrentTime());
        this.consultationDoctorMapper.updateById(consultationDoctor);
        ArrayList<String> memberList = new ArrayList<>();
        memberList.add(consultationDoctor.getDoctorImId());
        this.messageService.memberDelete(consultation.getGroupImId(), "与会医生自己退出", memberList);
        this.messageService.sendMeg(consultationDoctor.getDoctorImId(), consultation.getApplyDoctorImId(),
                consultationDoctor.getDoctorName() + " 退出会诊");
    }

    @Override
    public void advise(ConsultationAdviseForm consultationAdviseForm) {
        ConsultationDoctor consultationDoctor = this.consultationDoctorMapper.findByConsultationIdAndDoctorId(consultationAdviseForm.getConsultationId(), consultationAdviseForm.getDoctorId());
        if (consultationDoctor == null) {
            throw new ErrorCodeException(ErrorCode._1002);
        }
        int status = consultationDoctor.getStatus();
        if (status == ConsultationDoctorStatus.success || status == ConsultationDoctorStatus.reviewSuccess) {
            Consultation consultation = this.consultationMapper.findById(consultationAdviseForm.getConsultationId());
            int currentTime = StringUtil.getCurrentTime();
            if (StringUtil.isBlank(consultationDoctor.getAdvise())) {//新增会诊建议
                consultationDoctor.setAdviseAddtime(currentTime);
            }
            consultationDoctor.setAdviseModifytime(currentTime);
            consultationDoctor.setAdvise(consultationAdviseForm.getAdvise());
            this.consultationDoctorMapper.updateById(consultationDoctor);

            this.messageService.sendMeg(consultationDoctor.getDoctorImId(), consultation.getApplyDoctorImId(),
                    consultationDoctor.getDoctorName() + " 提交会诊建议");
            return;
        }
        throw new ErrorCodeException(ErrorCode._1003);
    }

    @Override
    public void conclusion(ConsultationConclusionForm consultationConclusionForm) {
        Consultation consultation = this.consultationMapper.findById(consultationConclusionForm.getConsultationId());
        int currentTime = StringUtil.getCurrentTime();
        consultation.setConclusion(consultationConclusionForm.getConclusion());
        consultation.setStatus(ConsultationStatus.finish);
        consultation.setFinishTime(currentTime);
        this.consultationMapper.updateById(consultation);
        List<ConsultationDoctor> consultationDoctors = this.consultationDoctorMapper.findByConsultationId(consultationConclusionForm.getConsultationId());
        for (ConsultationDoctor consultationDoctor : consultationDoctors) {
            if (consultationDoctor.getStatus() == ConsultationDoctorStatus.reviewSuccess || consultationDoctor.getStatus() == ConsultationDoctorStatus.reviewSuccess) {
                this.messageService.sendMeg(consultation.getApplyDoctorImId(), consultationDoctor.getDoctorImId(),
                        getFinishConsultationDoctorContent(consultation.getApplyDoctorName(), consultationDoctor.getDoctorName()));
            }
        }
    }

    @Override
    public List<ConsultationAdviseDto> adviseList(String consultationId) {
        List<ConsultationDoctor> consultationDoctorList = this.consultationDoctorMapper.findByConsultationId(consultationId);
        ArrayList<ConsultationAdviseDto> consultationAdviseDtos = new ArrayList<>();
        for (ConsultationDoctor consultationDoctor : consultationDoctorList) {
            ConsultationAdviseDto consultationAdviseDto = new ConsultationAdviseDto();
            BeanUtils.copyProperties(consultationDoctor, consultationAdviseDto);
            consultationAdviseDtos.add(consultationAdviseDto);
        }
        return consultationAdviseDtos;
    }

    @Override
    public ConsultationAdviseDto getAdvise(ConsultationGetAdviseForm consultationGetAdviseForm) {
        ConsultationDoctor consultationDoctor = this.consultationDoctorMapper.findByConsultationIdAndDoctorId(consultationGetAdviseForm.getConsultationId(), consultationGetAdviseForm.getDoctorId());
        if (consultationDoctor == null) {
            throw new ErrorCodeException(ErrorCode._1002);
        }
        ConsultationAdviseDto consultationAdviseDto = new ConsultationAdviseDto();
        BeanUtils.copyProperties(consultationDoctor, consultationAdviseDto);
        return consultationAdviseDto;
    }

    @Override
    public ConsultationInfoDto info(String consultationId) {
        Consultation consultation = this.consultationMapper.findById(consultationId);
        List<ConsultationDoctor> consultationDoctorList = this.consultationDoctorMapper.findByConsultationId(consultationId);
        ArrayList<ConsultationAdviseDto> consultationAdviseDtos = new ArrayList<>();
        List<String> doctorList = new ArrayList<>();
        for (ConsultationDoctor consultationDoctor : consultationDoctorList) {
            int status = consultationDoctor.getStatus();
            if (status == ConsultationDoctorStatus.success || status == ConsultationDoctorStatus.reviewSuccess) {
                doctorList.add(consultationDoctor.getDoctorName());
                if (StringUtil.isNotBlank(consultationDoctor.getAdvise())) {
                    ConsultationAdviseDto consultationAdviseDto = new ConsultationAdviseDto();
                    BeanUtils.copyProperties(consultationDoctor, consultationAdviseDto);
                    consultationAdviseDtos.add(consultationAdviseDto);
                }
            }
        }
        ArrayList<ConsultationDetailDto> ConsultationDetailDtos = new ArrayList<>();
        List<ConsultationDetail> consultationDetailList = this.consultationDetailMapper.findByConsultationId(consultationId);
        for (ConsultationDetail consultationDetail : consultationDetailList) {
            ConsultationDetailDto consultationDetailDto = new ConsultationDetailDto();
            BeanUtils.copyProperties(consultationDetail, consultationDetailDto);
            ConsultationDetailDtos.add(consultationDetailDto);
        }
        ConsultationInfoDto consultationInfoDto = new ConsultationInfoDto();
        BeanUtils.copyProperties(consultation, consultationInfoDto);
        consultationInfoDto.setConsultationId(consultation.getId());
        consultationInfoDto.setConsultationAdviseList(consultationAdviseDtos);
        consultationInfoDto.setConsultationDetailList(ConsultationDetailDtos);
        consultationInfoDto.setDoctorList(doctorList);
        return consultationInfoDto;
    }

    @Override
    public List<ConsultationDoctorRankingDto> doctorRanking(String doctorId) {
        List<ConsultationDoctorRankingDto> consultationDoctorRankingDtos = this.consultationMapper.calcConsultationDoctorRanking(doctorId);
        ArrayList<ConsultationDoctorRankingDto> consultationDoctorRankingDto = new ArrayList<>();
        if(consultationDoctorRankingDtos != null && consultationDoctorRankingDtos.size() > 0){
            ConsultationVerifyForm consultationVerifyForm = new ConsultationVerifyForm();
            ArrayList<String> doctorIdList = new ArrayList<>();
            for(ConsultationDoctorRankingDto c : consultationDoctorRankingDtos){
                doctorIdList.add(c.getDoctorId());
            }
            consultationVerifyForm.setConsultationId(null);
            consultationVerifyForm.setDoctorIds(doctorIdList);
            List<ConsultationVerifyDTO> consultationVerifyDTOS = this.hospitalServerFeignClient.consultationCheck(consultationVerifyForm);
            for(ConsultationVerifyDTO consultationVerifyDTO : consultationVerifyDTOS){
                ConsultationDoctor consultationDoctor = this.consultationVerifyDTO2ConsultationDoctor(consultationVerifyDTO);
                ConsultationDoctorRankingDto consultationDoctorRankingDto1 = new ConsultationDoctorRankingDto();
                BeanUtils.copyProperties(consultationDoctor, consultationDoctorRankingDto1);
                consultationDoctorRankingDto.add(consultationDoctorRankingDto1);
            }
        }

        return consultationDoctorRankingDto;
    }

    @Override
    public ConsultationInfoDto getByGroupImId(String groupImId) {
        Consultation consultation = this.consultationMapper.findByGroupImId(groupImId);
        if (consultation == null) {
//            throw new ErrorCodeException(ErrorCode._1001);
            return null;
        }
        return this.info(consultation.getId());
    }
}
